package com.wanmait.travel.controller.user;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.wanmait.travel.dao.ScenicDAO;
import com.wanmait.travel.util.ShopCar;
import com.wanmait.travel.vo.Scenic;
import com.wanmait.travel.vo.ScenicTickitType;
import com.wanmait.travel.vo.Userinfo;
@WebServlet("/ShopCarController")
public class ShopCarController extends HttpServlet{
	private ScenicDAO scenicDAO =ScenicDAO.getDAO();
	
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		String action=request.getParameter("action");
		if(action==null) {
			return;
		}
		switch(action) {
		case "buy":
			this.buy(request, response);
			break;
			
		case "addCount":
			this.addCount(request, response);
			break;
			
		case "diffCount":
			this.diffCount(request, response);
			break;
			
		case "clear":
			this.clear(request, response);
			break;
		case "deleteScenic":
			this.deleteScenic(request, response);
			break;
		}
		
	}
	
	
	protected void buy(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Userinfo userinfo=(Userinfo)request.getSession().getAttribute("userinfo");
		if(userinfo!=null) {
		String scenicIdStr=request.getParameter("scenicId");
		int scenicId=Integer.parseInt(scenicIdStr);
		String tcv=request.getParameter("tcv");
		String tov=request.getParameter("tov");
		String tmv=request.getParameter("tmv");
		List<Scenic> scenic = scenicDAO.findById(scenicId);
		ScenicTickitType stt=scenicDAO.findTickitTypeAll(scenic);
		ShopCar shopCar=(ShopCar)request.getSession().getAttribute("shopCar");
		if(shopCar==null) {
			shopCar =new ShopCar();
			request.getSession().setAttribute("shopCar", shopCar);
		}
		shopCar.addScenicTickitType(stt,tcv,tov,tmv);
		}else {
			response.getWriter().print(1);
		}
	}
	
	protected void addCount(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String scenicIdStr=request.getParameter("scenicId");
		int scenicId=Integer.parseInt(scenicIdStr);
		Integer tickitChildCount=null;
		Integer tickitOldCount=null;
		Integer tickitManCount=null;
		
		String tickitChildStr=request.getParameter("tcCount");
		if(tickitChildStr!=null) {
		tickitChildCount=Integer.parseInt(tickitChildStr);
		}
		
		String tickitOldStr=request.getParameter("toCount");
		if(tickitOldStr!=null) {
		tickitOldCount=Integer.parseInt(tickitOldStr);
		}
		
		String tickitManStr=request.getParameter("tmCount");
		if(tickitManStr!=null) {
		 tickitManCount=Integer.parseInt(tickitManStr);
		}
		
		ShopCar shopCar=(ShopCar)request.getSession().getAttribute("shopCar");
		if(shopCar==null) {
			shopCar=new ShopCar();
			request.getSession().setAttribute("shopCar", shopCar);
		}
		shopCar.addCount(scenicId,tickitChildCount,tickitManCount,tickitOldCount);
	}
	
	protected void diffCount(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String scenicIdStr=request.getParameter("scenicId");
		int scenicId=Integer.parseInt(scenicIdStr);
		Integer tickitChildCount=null;
		Integer tickitOldCount=null;
		Integer tickitManCount=null;
		
		String tickitChildStr=request.getParameter("tcCount");
		if(tickitChildStr!=null) {
		tickitChildCount=Integer.parseInt(tickitChildStr);
		}
		
		String tickitOldStr=request.getParameter("toCount");
		if(tickitOldStr!=null) {
		tickitOldCount=Integer.parseInt(tickitOldStr);
		}
		
		String tickitManStr=request.getParameter("tmCount");
		if(tickitManStr!=null) {
		 tickitManCount=Integer.parseInt(tickitManStr);
		}
		
		ShopCar shopCar=(ShopCar)request.getSession().getAttribute("shopCar");
		if(shopCar==null) {
			shopCar=new ShopCar();
			request.getSession().setAttribute("shopCar", shopCar);
		}
		shopCar.addCount(scenicId,tickitChildCount,tickitManCount,tickitOldCount);
	}
	
	protected void clear(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	ShopCar shopCar=(ShopCar)request.getSession().getAttribute("shopCar");
	if(shopCar!=null) {
		shopCar.clear();
	}
	}
	
	protected void deleteScenic(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String scenicIdStr=request.getParameter("scenicId");
		Integer scenicId=Integer.parseInt(scenicIdStr);
		String tickitTypeStr=request.getParameter("tickitType");
		Integer tickitType=Integer.parseInt(tickitTypeStr);
		ShopCar shopCar=(ShopCar)request.getSession().getAttribute("shopCar");
		scenicDAO.deleteScenic(shopCar,scenicId,tickitType);
	}
	
	
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}
}
